package com.tencent.examples.blueking.mongo.service;

import com.mongodb.BasicDBObject;
import com.tencent.bk.utils.blueking.tools.CommonSugar;
import com.tencent.bk.utils.blueking.tools.StringConverters;
import com.tencent.examples.blueking.mongo.extend.biz.CrudBiz;
import com.tencent.examples.blueking.extend.QueryParam;
import com.tencent.examples.blueking.mongo.model.HostBase;
import com.tencent.examples.blueking.mongo.model.ObjectBase;
import com.tencent.examples.blueking.mongo.repository.HostBaseRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.MatchOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("HostBaseService")
public class HostBaseService extends CrudBiz<HostBaseRepository> {

    @Resource
    private MongoTemplate mongoTemplate;

    public List<ObjectBase> getQyInfo(String bk_obj_id) {
        List<ObjectBase> objectBaseList = dao.findObjectBaseTotal();
        return objectBaseList;
    }

    public Long getHostTotal(Map<String, String> map) {
        Query query = new Query();

        //主机统计
//        String bk_host = map.get("bk_host");
//        if (StringUtils.isNotBlank(bk_host)) {
//            query.addCriteria(Criteria.where());
//        }

        //SLA统计
        String bk_sla = map.get("bk_sla");
        if (StringUtils.isNotBlank(bk_sla)) {
            query.addCriteria(Criteria.where("bk_sla").is(bk_sla));
        }
        //业务名称排序统计
        String com_app_id = map.get("com_app_id");
        if (StringUtils.isNotBlank(com_app_id)) {
            query.addCriteria(Criteria.where("com_app_id").is(StringConverters.ToInteger(com_app_id)));
        }

        //ISP统计
        String bk_isp_name = map.get("bk_isp_name");
        if (StringUtils.isNotBlank(bk_isp_name)) {
            query.addCriteria(Criteria.where("bk_isp_name").is(bk_isp_name));
        }

        //OS类型统计
        String bk_os_type = map.get("bk_os_type");
        if (StringUtils.isNotBlank(bk_os_type)) {
            query.addCriteria(Criteria.where("bk_os_type").is(bk_os_type));
        }
        //OS类型统计-字符
        String bk_os_name = map.get("bk_os_name");
        if (StringUtils.isNotBlank(bk_os_name)) {
            query.addCriteria(Criteria.where("bk_os_name").is(bk_os_name));
        }

        //环境统计
        String com_evn = map.get("com_evn");
        if (StringUtils.isNotBlank(com_evn)) {
            query.addCriteria(Criteria.where("com_evn").is(com_evn));
        }

        //状态统计
        String com_status = map.get("com_status");
        if (StringUtils.isNotBlank(com_status)) {
            query.addCriteria(Criteria.where("com_status").is(com_status));
        }

        //模块ID
        String bk_obj_id = map.get("bk_obj_id");
        if (StringUtils.isNotBlank(bk_obj_id)) {
            query.addCriteria(Criteria.where("bk_obj_id").is(bk_obj_id));
        }

        //主机信息
        String server_type = map.get("server_type");
        if (StringUtils.isNotBlank(server_type)) {
            query.addCriteria(Criteria.where("server_type").is(server_type));
        }
        Long to = mongoTemplate.count(query, HostBase.class);
        return to;
    }

    /**
     * 分页查询
     *
     * @param queryParam
     * @return
     */
    public List<HostBase> searchAll(QueryParam queryParam) {
        Query query = new Query();
        query.with(new Sort(new Sort.Order(CommonSugar.getTypedDefault(queryParam.getSort(), Sort.Direction.DESC), CommonSugar.getTypedDefault(queryParam.getSortby(), "id"))));
        Map<String, String> dataMap = queryParam.getSqlMap();
        String bk_host_id = dataMap.get("bk_host_id");
        if (StringUtils.isNotEmpty(bk_host_id)) {
            query.addCriteria(Criteria.where("bk_host_id").is(bk_host_id));
        }
        QueryParam.pagination(query, queryParam);//分页设置
        return mongoTemplate.find(query, HostBase.class);
    }

    /**
     * 原生多表查询--分页
     *
     * @param queryParam
     * @return
     */
    public List<BasicDBObject> getAggregation(QueryParam queryParam, Pageable pageable) {
        Map<String, String> dataMap = queryParam.getSqlMap();
        Criteria criteria = new Criteria();
        String bk_host_id = dataMap.get("bk_host_id");
        if (StringUtils.isNotEmpty(bk_host_id)) {
            criteria.and("bk_host_id").is(bk_host_id);
        }
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),//查询条件
                Aggregation.sort(pageable.getSort()),//排序字段
                Aggregation.skip(pageable.getPageNumber() > 1 ? (pageable.getPageNumber() - 1) * pageable.getPageSize() : 0),//分页操作，pageNumber为当前页数，pageSize为每页显示条数
                Aggregation.limit(pageable.getPageSize()));//页数
        AggregationResults<BasicDBObject> ret = mongoTemplate.aggregate(aggregation, "cc_HostBase", BasicDBObject.class);
        return ret.getMappedResults();
    }

    /**
     * 原生多表查询--分页
     *
     * @param queryParam
     * @return
     */
    public List<BasicDBObject> getAggregation1(QueryParam queryParam, Pageable pageable) {
        Criteria criteria = new Criteria();
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),//查询条件
                Aggregation.sort(pageable.getSort()),//排序字段
                Aggregation.skip(pageable.getPageNumber() > 1 ? (pageable.getPageNumber() - 1) * pageable.getPageSize() : 0),//分页操作，pageNumber为当前页数，pageSize为每页显示条数
                Aggregation.limit(pageable.getPageSize()));//页数
        AggregationResults<BasicDBObject> ret = mongoTemplate.aggregate(aggregation, "cc_ApplicationBase", BasicDBObject.class);
        return ret.getMappedResults();
    }

    /**
     * 原生多表查询--不带分页
     *
     * @param queryParam
     * @return
     */
    public List<BasicDBObject> getAggregation(QueryParam queryParam) {
        Map<String, String> dataMap = queryParam.getSqlMap();
        Criteria criteria = new Criteria();
        String bk_host_id = dataMap.get("bk_host_id");
        if (StringUtils.isNotEmpty(bk_host_id)) {
            criteria.and("bk_host_id").is(bk_host_id);
        }
        MatchOperation matchOperation = Aggregation.match(criteria);//lookupAgg一定要在matchOperation的前面 两者顺序颠倒就过滤不了
        Aggregation aggregation = Aggregation.newAggregation(matchOperation);
        AggregationResults<BasicDBObject> ret = mongoTemplate.aggregate(aggregation, "cc_HostBase", BasicDBObject.class);
        return ret.getMappedResults();
    }
}
